package wangjg.commons.bean;

import java.util.Comparator;

@SuppressWarnings({"rawtypes","unchecked"})
public class BeanComparator1 implements Comparator{
	//排序属性
	private String orderBy;
	private int asc = 1;
	
	public BeanComparator1(String orderBy, boolean asc) {
		this.orderBy = orderBy;
		this.asc = asc?1:-1;
	}

	public int compare(Object bean1, Object bean2) {
		try {
			Object pv1 = BeanUtil.getProperty(bean1, orderBy);
			Object pv2 = BeanUtil.getProperty(bean2, orderBy);
			if(pv1 instanceof Comparable && pv2 instanceof Comparable){
				Comparable c1 = (Comparable)pv1;
				Comparable c2 = (Comparable)pv2;
				return this.asc * c1.compareTo(c2);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return 0;
	}
}
